package com.li.controller;
import com.li.pojo.Dept;
import com.li.pojo.Student;
import com.li.service.impl.DeptServiceImpl;
import com.li.service.impl.StudentServiceImpl;
import com.li.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * (user)表控制层
 *
 * @author xxxxx
 */
@Controller
@RequestMapping("/stu")
public class StudentController {
    /**
     * 服务对象
     */
    @Autowired
    private StudentServiceImpl studentService;
    @Autowired
    private DeptServiceImpl deptService;
    @Resource
    private List<Dept> dlist;


    /**
     * 通过主键查询单条数据
     *
     * @param
     * @return 单条数据
     */

    @GetMapping("index")
    public String index(){
        return "index1";
    }

    @RequestMapping("query")
    public String query(Model model,@RequestParam Map<String,Object> params) {
        String name= (String) params.get("name");
        PageUtils page = studentService.queryPage(params);
        model.addAttribute("name",name);
        model.addAttribute("smn",page);
        return "jsp/list";
    }

    @GetMapping("/perAdd")
    public String period(Model model) {
        dlist = deptService.list();
        model.addAttribute("de",dlist);
        return "jsp/add";
    }

    @PostMapping("/add")
    public String add(Student student) {
        studentService.saveOrUpdate(student);
        return "redirect:/stu/query" ;
    }

    @RequestMapping("del")
    public String del(Student  student){
        //userService.removeById(id);//物理删除
        student.setStatus(0);
        studentService.updateById(student);//逻辑删除
        return "redirect:/stu/query";
    }
    @RequestMapping("dels")
    public String dels(@RequestParam(value = "ids",required = false) String  ids){
        String[] arr =ids.split(",");
        List<Student> students = new ArrayList<Student>();
        for (String id:arr) {
            Student u = new Student();
            u.setId(Integer.valueOf(id));
            u.setStatus(0);
            students.add(u);
        }

        // userService.removeByIds(is);//物理删除
        studentService.updateBatchById(students);//逻辑删除，批量修改
        return "redirect:/stu/query";
    }

    @GetMapping("/del")
    public String  del(Integer id) {
        studentService.removeById(id);
        return "redirect:/stu/query";
    }

    @GetMapping("/edit")
    public String edit( Integer id, Model model) {
        Student student=studentService.getById(id);
        model.addAttribute("us",student);
        dlist = deptService.list();
        model.addAttribute("de",dlist);
        return "jsp/add";
    }





/*    @RequestMapping(value = "query", method = RequestMethod.GET)
    public String queryAllPaper(@RequestParam(value="currentPage",defaultValue="1",required=false) int currentPage, Model model) {
        model.addAttribute("smn", studentService.findByPage(currentPage));//回显分页数据
        return "list";
    }*/
}
